﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using IFTRUE.FMS.Helper;
using MongoDB.Bson;
using System.Collections;

namespace IFTRUE.FMS.BLL
{
   /// <summary>
    /// 作用：用户信息操作类
    /// 作者：iftrue
    /// 日期：2013-08-19
   /// </summary>
  public  class UserInfo
    {
      private MongoCollection collection;
      public UserInfo()
      {
          collection = MongoDbFactory.MongoDatabase.GetCollection("UserInfo");
      }

      /// <summary>
      /// 新增用户信息
      /// </summary>
      /// <param name="userInfoModel"></param>
      /// <returns></returns>
      public bool Add(Model.UserInfo userInfoModel)
      {
          userInfoModel.Id = MongoDB.Bson.ObjectId.GenerateNewId();
          var result = collection.Insert<Model.UserInfo>(userInfoModel);
          return result.Ok;
      }

      /// <summary>
      /// 更新用户信息
      /// </summary>
      /// <param name="userModel">用户信息实体</param>
      /// <returns></returns>
      public bool Update(Model.UserInfo userInfoModel)
      {
          var query = MongoDB.Driver.Builders.Query.EQ("Account", userInfoModel.Account);
          //定义更新文档
          var update = MongoDB.Driver.Builders.Update.Set("Name", userInfoModel.Name).Set("Phone", userInfoModel.Phone).Set("Folder", userInfoModel.Folder);
          if (userInfoModel.Account != "admin")
          {
              update = update.Set("IsUse", userInfoModel.IsUse);
          }
          var result = collection.Update(query, update);
          return result.Ok;
      }

     /// <summary>
      /// 更新用户信息(单个字段)
     /// </summary>
      /// <param name="userModel">用户信息实体</param>
      /// <param name="fieldName">要更新的字段名称</param>
      /// <param name="type">要更新的字段类型</param>
     /// <returns></returns>
      public bool Update(Model.UserInfo userInfoModel,string fieldName,Type type)
      {   
          var query = MongoDB.Driver.Builders.Query.EQ("Account", userInfoModel.Account);
          //定义更新文档
          string str=ObjectHelper.GeValueByName<Model.UserInfo>(userInfoModel, fieldName);

          IMongoUpdate update = null;
          if (type.Name == "DateTime")
          {
              update = MongoDB.Driver.Builders.Update.Set(fieldName, Convert.ToDateTime(str));
          }
          else
          {
              update = MongoDB.Driver.Builders.Update.Set(fieldName, str);
          }
          var result = collection.Update(query, update);
          return result.Ok;
      }

      /// <summary>
      /// 获取用户信息
      /// </summary>
      /// <param name="account">用户帐号</param>
      /// <param name="pwd">用户密码</param>
      /// <returns></returns>
      public Model.UserInfo Get(string account, string pwd)
      {
          //定义查询条件
          var query = MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ("Account", account), MongoDB.Driver.Builders.Query.EQ("Password",Helper.Encrypt.MD5(pwd)));

          //查询指定查询条件的全部数据
          Model.UserInfo result = collection.FindOneAs<Model.UserInfo>(query);
          return result;
      }

      /// <summary>
      /// 获取用户信息
      /// </summary>
      /// <param name="account">用户帐号</param>
      /// <returns></returns>
      public Model.UserInfo Get(string account)
      {
          //定义查询条件
          var query = MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ("Account", account));

          //查询指定查询条件的全部数据
          Model.UserInfo result = collection.FindOneAs<Model.UserInfo>(query);
          return result;
      }

      /// <summary>
      /// 帐号是否已存在
      /// </summary>
      /// <param name="account"></param>
      /// <returns></returns>
      public bool Exists(string account)
      {
          //定义查询条件
          var query = MongoDB.Driver.Builders.Query.EQ("Account", account);

          //查询指定查询条件的全部数据
          Model.UserInfo result = collection.FindOneAs<Model.UserInfo>(query);
          if (result != null)
          {
              return true;
          }
          return false;
      }

      /// <summary>
      /// 获取分页数据
      /// </summary>
      /// <param name="currIndex">当前页索引</param>
      /// <param name="pageSize">页行数</param>
      /// <param name="current">查询条件</param>
      /// <returns></returns>
      public string GetDataByPage(int currIndex, int pageSize, Hashtable ht)
      {
          MongoDB.Driver.IMongoQuery[] querys = null;
          if (ht != null && ht.Count != 0)
          {
              querys = new MongoDB.Driver.QueryDocument[ht.Count];
              int i = 0;
              foreach (DictionaryEntry de in ht)
              {
                  if (de.Key.ToString() == "Account")
                  {
                      querys[i] = MongoDB.Driver.Builders.Query.EQ("Account", Convert.ToInt32(de.Value.ToString()));
                  }
                  else if (de.Key.ToString() == "Name")
                  {
                      querys[i] = MongoDB.Driver.Builders.Query.GTE("Name", Convert.ToDateTime(de.Value.ToString()));
                  }
                  i++;
              }
          }
          long recordCount = 0;
          var result = new Common().GetMongoCursorByPage<Model.UserInfo>(collection, (querys == null) ? null : MongoDB.Driver.Builders.Query.And(querys), MongoDB.Driver.Builders.SortBy.Ascending("Account"), currIndex, pageSize, ref recordCount);
          StringBuilder s = new StringBuilder();
          if (result != null)
          {
              foreach (var v in result)
              {
                  s.Append(string.Format("<tr target='pid' rel='" + v.Account + "'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>",
                       v.Account, v.Name, v.Phone, v.Folder, v.IsUse ? "启用" : "禁用"));
              }
          }
          return recordCount.ToString() + "|" + s.ToString();
      }
    }
}
